package server.handler.groupHandler;

import com.alibaba.fastjson.JSONObject;
import common.EnvelopeMessage;
import common.messages.HistoryMessage;
import server.dao.MessageDao;
import server.handler.SimpleHandler;
import server.session.Session;

import java.util.ArrayList;

public class GroupHistoryHandler implements SimpleHandler {
    @Override
    public void handle(EnvelopeMessage msg, Session session) {
        JSONObject body = msg.getBody();
        Integer userId = body.getInteger("userId");
        String groupName = body.getString("groupName");
        Integer groupId = body.getInteger("groupId");
        ArrayList<HistoryMessage> messages = (ArrayList<HistoryMessage>) new MessageDao().getGroupMessages(groupId);
        JSONObject object = new JSONObject();
        object.put("groupName",groupName);
        object.put("messages",messages);
        EnvelopeMessage back = new EnvelopeMessage("history","group",object);
        String json = JSONObject.toJSONString(back);
        System.out.println("服务端返回"+json);
        session.send(json);

    }
}
